import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $http, $fn } = window
// ===================================================================== component
class Index extends React.Component{
	state = {
		formData:[]
	}
	
	open =  async code => {
		this.modal.open({ formRef: this.formRef })
		
		// 回显默认值
		const data = await $fn.dataSave('printer')
		
		await this.getModalSelect(code, data)
		
		if($fn.hasObject(data)){
			const stack = {}
			for(let i in data){
				const s = data[i].select
				if(s){
					stack[data[i].name] = s
				}
			}
			this.formRef.setValue(stack)
		}
	}
	
	getModalSelect = async (code, data)=> {
		const { formData } = this.state
		if(!$fn.hasObject(data)){

			data =  await $http.pull(null,'printer/tmplList', { loading:true, dataName:null })
			
			$fn.dataSave('printer', data)
			
			data = code ? { [code]: data[code] } : data
		}
		
		if(!$fn.hasObject(formData)){
			const stack = []
			for(let i in data){
				const v = data[i]
				stack.push({
					label: v['remark'],	
					name: v['name'], 
					data: v['printer'],
					nameStr:'printer_name',
					idStr: 'printer_code',
					type:'select',
					full:true,
				})
			}
			this.setState({ formData: stack })
		}
	}
	
	render(){
		return FormModal.call(this,{ title: '打印机设置', labelWidth:'s6', width:600, onSubmit:async (param, close)=>{
			const data = await $fn.dataSave('printer')
			for(let i in data){
				for(let k in param){
					if(data[i]['name'] === k ){
						data[i].select = param[k]
					}
				}
			}
			$fn.dataSave('printer', data)
			close()
		}})
	}
}

export default Index